
#include <BMouseEvent>
#include <BFont>
#include <BImage>
#include <BPainter>
#include <BViewer>
#include <BThread>
#include <BSurface>
#include <BSphere>
#include <BCamera>
#include <BViewer>
#include <BScene>
#include <BProgram>
#include <BMaterial>
#include <BHBoxLayout>
#include <BGridLayout>
#include <BApplication>
#include <BRoundHandler>
#include <BMainWindow>
#include <BDialog>
#include <BButton>
#include <BScrollBar>

using namespace BWE;

const char* sheet = R"(
	border:1px;
	TrayDialog:title{
		margin-left:20px;
		margin-right:20px;
	}
)";

class MyWidget : public BWidget
{
public:
	MyWidget(BWidget* parent = 0) : BWidget(parent)
	{

	}
	~MyWidget()
	{

	}

protected:
	virtual void paintEvent(const BEvent& event)
	{
		int width = this->width();
		int height = this->height();

		BPainter painter(this);
		painter.setColor(222, 222, 222);
		painter.drawRect(this->centerRect());

		painter.setColor(100, 255, 100);
		painter.setLineSmooth(true);
		painter.setLineWidth(3);
		BPoint points[4];
		points[0].set(10, 10);
		points[1].set(60, 50);
		points[2].set(66, 90);
		points[3].set(20, 70);
		painter.drawLineLoop(points, 4);
	}

};

class AlignDialog : public BDialog
{
public:
	AlignDialog(BWidget* parent = 0) : BDialog(parent)
	{
		BGridLayout* gridLayout = new BGridLayout(this);
		gridLayout->setRowCount(3);
		gridLayout->setColumnCount(3);

		BButton* button0 = new BButton('0');

		BButton* button1 = new BButton('1');
		BButton* button2 = new BButton('2');
		BButton* button3 = new BButton('3');
		BButton* button4 = new BButton('4');

		BButton* button5 = new BButton('5');
		BButton* button6 = new BButton('6');
		BButton* button7 = new BButton('7');
		BButton* button8 = new BButton('8');

		gridLayout->setWidget(1, 1, button0);
		
		gridLayout->setWidget(0, 0, button1);
		gridLayout->setWidget(0, 2, button2);
		gridLayout->setWidget(2, 0, button3);
		gridLayout->setWidget(2, 2, button4);

		gridLayout->setWidget(0, 1, button5);
		gridLayout->setWidget(1, 0, button6);
		gridLayout->setWidget(1, 2, button7);
		gridLayout->setWidget(2, 1, button8);

		for (int i = 0; i < gridLayout->count(); i++)
		{
			BWidget* widget = gridLayout->widget(i);
			widget->setFixedSize(100);
			widget->setSizePolicy(Policy_Fixed | Policy_Shrinking);
		}
	}
	~AlignDialog()
	{

	}

};

class TrayDialog : public BDialog
{
	BOBJECT(TrayDialog);
public:
	TrayDialog(const BString& title)
	{
		this->setTitle(title);
		this->setFrameless(true);
		this->setBorder(10);
		this->setPerch(10);

		_inside = false;
		_pressed = false;
		_image = new BImage("data/image/Multimedia_Icons_009.png");

		_button = new BButton("button", this);
		_button->setRect(100, 100, 80, 80);
		_button->setRadius(26);
		_button->setName("button");
		_button->setStyleSheet("border-color-hover:red");
		_button->setSizePolicy(Policy_Preferred);
		_button->setMargin(0);

		MyWidget* widget = new MyWidget(this);
		widget->setRect(300, 50, 100, 100);
		widget->setName("scroll");
		widget->setSizePolicy(Policy_Preferred);

		//BHBoxLayout* hlayout = new BHBoxLayout(this);
		//hlayout->addWidget(button);
		//hlayout->addWidget(scrollBar);

		connect(_button, Signal_Clicked, &TrayDialog::slotButtonClicked);
	}
	~TrayDialog()
	{

	}

protected:
	void slotButtonClicked(BObject* object, const BValue& value)
	{
		static AlignDialog* dialog = new AlignDialog(this);
		dialog->execute();
	}

protected:
	virtual void paintEvent(const BEvent& event)
	{
		//BDialog::paintEvent(event);

		BRect crect = this->centerRect();
		
		BPainter painter(this);
		painter.drawPerch();

		painter.setLineWidth(3);
		painter.setLineSmooth(true);
		if (_inside)
			painter.setColor(200, 0, 200);
		else
			painter.setColor(200, 0, 0);

		int size = int(crect.width() * 0.4f);
		painter.drawEllipse(crect.center(), BSize(size, size));

		size = int(crect.width() * 0.3f);
		painter.fillEllipse(crect.center(), BSize(size, size));

		painter.bind(_image);
		painter.setColor(0, 255, 0, 128);
		painter.fillRect(crect.left(), crect.top(), crect.width() / 2, crect.height());
		painter.unbind();

		painter.setLineWidth(1);
		painter.setColor(0, 0, 0);
		painter.drawRect(_button->rect());
	}

private:
	bool		_inside;
	bool		_pressed;
	BPoint		_pressedPos;
	BImage*		_image;
	BButton*	_button;
};

int main(int argc, char** argv)
{
	BApplication app;
	app.setStyleSheet(sheet);
	
	TrayDialog window("Atray");
	window.setRadius(40);
	window.setFrameless(true);
	window.setBorder(6);
	window.setMargin(2, 2, 2, 2);
	window.setSize(500, 400);
	window.show();

	return app.execute();
}
